﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BLL;
using DataAccess;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using QuanLyKhachSan.SplashScreen;
using QuanLyKhachSan.Tabs;


namespace QuanLyKhachSan{
    public partial class MainForms : RibbonForm
    {
        public MainForms()
        {
            InitializeComponent();
            TabManagers.tabParent = xtraTabControlMain;
            CaiDatGiaoDien();
        }

        private void CaiDatGiaoDien()
        {
            ribbon.SelectedPage = rbGioiThieu;DevExpress.Skins.SkinManager.EnableFormSkins();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabKhachHang>();
            splashManager.CloseWaitForm();
        }
        private void xtraTabControlMain_CloseButtonClick(object sender, EventArgs e)
        {
            splashManager.ShowWaitForm();
            var arg = e as ClosePageButtonEventArgs;
            TabManagers.CloseDynamicTab(arg.Page as XtraTabPage);
            splashManager.CloseWaitForm();
            
        }
        private void btnDichVu_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabDichVu>();
            splashManager.CloseWaitForm();

        }

        private void btnHoaDonDV_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabHoaDonDichVu>();
            splashManager.CloseWaitForm();
        }

        private void btnChiTietHDDV_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabChiTietHoaDonDV>();
            splashManager.CloseWaitForm();

        }

        private void btnThongKe_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabDoanhThu>();
            splashManager.CloseWaitForm();
        }

        private void btnPhanQuyen_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabPhanQuyen>();
            splashManager.CloseWaitForm();
        }

        private void btnNhanVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabNhanVien>();
            splashManager.CloseWaitForm();}

        public void LoadQuyen()
        {
            splashManager.ShowWaitForm();
            Commons.Current_.TrangThai = "Đang đăng nhập";
            new TaiKhoanManager().CapNhat(Commons.Current_);
            btnDangNhap.Enabled = btnNhatKy.Enabled = false;
            btnDangXuaat.Enabled = true;
            btnXemThongTin.Enabled = true;
            btnDoiMatKhau2.Enabled = true;

            var nh = new NhanVienManager().LayNhomTk().Result.Where(t => t.MaNhom.Equals(Commons.Current_.MaNhom)).ToList()[0];
            splashManager.CloseWaitForm();
            if (nh.TenNhom == "Administrator")
            {
                XtraMessageBox.Show("Chào mừng trở lại hệ thống, Admin!", "Thông báo", MessageBoxButtons.OK);
                btnNhatKy.Enabled = true;
                ChekFalseOrTrue(true);
            }
            else
            {
                XtraMessageBox.Show("Bạn đã đăng nhập thành công bằng tài khoản " + Commons.Current_.Username, "Thông báo", MessageBoxButtons.OK);
                ChekFalseOrTrue(false);
                var lsNhomRx = new NhanVienManager().LayNhomTk().Result;
                var listPhanQuyen = nh.PhanQuyens.ToList();
                var listNhomTK = new List<PhanQuyen>();
                var pq = new string[5];
                for (int i = 0; i < listPhanQuyen.Count; i++)
                {
                    listNhomTK = new PhanQuyenManager().LayDanhSach().Result.Where(q => q.MaQuyen.Equals(listPhanQuyen[i].MaQuyen)).ToList();
                    for (int j = 0; j < listNhomTK.Count; j++)
                    {
                        if (listNhomTK[j].MaNhom.Equals(lsNhomRx[0].MaNhom))
                            pq[0] = "1";
                        if (listNhomTK[j].MaNhom.Equals(lsNhomRx[1].MaNhom))
                            pq[1] = "1";
                        if (listNhomTK[j].MaNhom.Equals(lsNhomRx[2].MaNhom))
                            pq[2] = "1";
                        if (listNhomTK[j].MaNhom.Equals(lsNhomRx[3].MaNhom))
                            pq[3] = "1";
                    }
                }

                if (pq[0] == "1")
                {
                    rbQLKhachHang.Enabled = true;
                    rbQLPhong.Enabled = true;
                }
                if (pq[1] == "1")
                    rbQLNhanVien.Enabled = true;
                if (pq[2] == "1")
                    rbQLDichVu.Enabled = true;
                if (pq[3] == "1")
                    rbQLThongKe.Enabled = true;
            }

        }

        private void ChekFalseOrTrue(bool condition)
        {
            rbQLKhachHang.Enabled = condition;
            rbQLPhong.Enabled = condition;
            rbQLNhanVien.Enabled = condition;
            rbQLDichVu.Enabled = condition;
            rbQLNguoiDung.Enabled = condition;
            rbQLCSDL.Enabled = condition;
            rbQLThongKe.Enabled = condition;
        }
        private void MainForms_Load(object sender, EventArgs e)
        {
            LoadQuyen();
            SkinHelper.InitSkinGallery(giaoDien);
        }

        private void SaoLuuPhucHoi(int value)
        {
            splashManager.ShowWaitForm();
            var frm = new FormSaoLuuPhucHoi(value);
            splashManager.CloseWaitForm();
            frm.ShowDialog();
        }

        private void btnPhucHoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            SaoLuuPhucHoi(1);
        }
        
        private void btnSaoLuu_ItemClick(object sender, ItemClickEventArgs e)
        {
           SaoLuuPhucHoi(0);
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }
        private void MainForms_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized==WindowState)
            {
                Hide();
            }
        }

        private void btnPhong_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabPhong>();
            splashManager.CloseWaitForm();
        }

        private void btnLoaiPhong_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabLoaiPhong>();
            splashManager.CloseWaitForm();
        }

        private void btnHoaDonThueP_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabHoaDonThuePhong>();
            splashManager.CloseWaitForm();
        }

        private void btnThietBiP_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabThietBiPhong>();
            splashManager.CloseWaitForm();

        }

        private void btnDangNhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                splashManager.ShowWaitForm();
                var frm = new frmLogin();
                splashManager.CloseWaitForm();
                frm.ShowDialog();
                LoadQuyen();
            }
            catch { }
        }

        private void btnDangXuaat_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            DialogResult dia =  XtraMessageBox.Show("Bạn muốn đăng xuất khỏi hệ thống?","Cảnh báo",MessageBoxButtons.YesNo);
            if (dia == DialogResult.Yes)
            {
                Commons.Current_.LanDangNhapCuoi = DateTime.Now;
                Commons.Current_.TrangThai = "Khả dụng";
                new TaiKhoanManager().CapNhat(Commons.Current_);
                ChekFalseOrTrue(false);
                btnDangXuaat.Enabled = btnXemThongTin.Enabled = btnDoiMatKhau2.Enabled = btnNhatKy.Enabled= false;
                btnDangNhap.Enabled = true;
                XuLyDongTabParent();
                Commons.Current_ = null;
            }
        }

        private void XuLyDongTabParent()
        {
            int index = TabManagers.tabParent.TabPages.Count;
            for(int i = index-1; i >=0;i--)
            {
                if(!TabManagers.tabParent.TabPages[i].Text.Equals("Giới thiệu"))
                    TabManagers.CloseDynamicTab(TabManagers.tabParent.TabPages[i]);
            }
        }

        private void btnThongKeSLKhach_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabTKSLKhach>();
            splashManager.CloseWaitForm();
        }

        private void btnDSTaiKhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabTaiKhoan>();
            splashManager.CloseWaitForm();

        }

        private void btnDoiMatKhau_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();

            try
            {
                var frmDoiMatKhau = new FormDoiMatKhau(Commons.Current_);
                splashManager.CloseWaitForm();
                frmDoiMatKhau.ShowDialog();
                if (frmDoiMatKhau.IsInserted)
                    XtraMessageBox.Show("Cập nhật mật khẩu thành công", "Thông báo", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
            }
            catch
            {
                XtraMessageBox.Show("Không tồn tại người dùng");
            }

        }

        private void btnCauHinh_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();

            var frmConfig = new frmCauHinh();
            splashManager.CloseWaitForm();

            frmConfig.ShowDialog();
        }

        private void btnBangLuong_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            TabManagers.AddDynamicTab<TabBangLuong>();
            splashManager.CloseWaitForm();
        }

        private void btnXemThongTin_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            var frmXemThongTin = new FormXemThongTin(Commons.Current_);
            splashManager.CloseWaitForm();
            frmXemThongTin.ShowDialog();

        }

        private void ribbon_SelectedPageChanged(object sender, EventArgs e)
        {
            var selectedPage = ribbon.SelectedPage;
            if(selectedPage.Text.ToLower().Equals("giới thiệu"))
                TabManagers.AddDynamicTab<TabGioiThieu>();

        }

        private void btnGioiThieu_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();

            var gioiThieu = new frmGioiThieu();
            splashManager.CloseWaitForm();
            gioiThieu.ShowDialog();

        }

        private void btnHuongDan_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();

            var frmHelp = new frmHuongDan();
            splashManager.CloseWaitForm();
            frmHelp.ShowDialog();

        }

        private void btnNhatKy_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashManager.ShowWaitForm();
            var frmNhatKy = new FormNhatKy();
            splashManager.CloseWaitForm();
            frmNhatKy.ShowDialog();

        }
    }
}